<?
class Database{
	private static $lastTable;
	private static $lastPK;
	public static function connectDB(){
		//$this->objConnect = new mysqli("workshopapp.com", "workshop", "Kl1f1jn8V8", "workshop_book","2222");
		mysql_connect("localhost", "workshop_book", "XPLocK");
		//$link = mysqli_connect("119.59.120.23","workshop_book","Kl1f1jn8V8","workshop_book","2222");	
		//$link = mysqli_connect("119.59.120.23","workshop","Kl1f1jn8V8","workshop_book","2222");
		//mysql_connect("workshopapp.com:2222","workshop","Kl1f1jn8V8");
		mysql_select_db("workshop_book");
		//echo $this->objConnect = $link;	
		mysql_query("SET NAMES UTF8");
		mysql_query("SET character_set_results=UTF8");
		mysql_query("SET character_set_client=UTF8");
		mysql_query("SET character_set_connection=UTF8");
	}
	
	public static function query($sql){
		$result = mysql_query($sql);
		if($result)
			return $result;	
		else
			echo "ERROR QUERY : ".$sql;
	}
	
	public static function fetchArray($query){
		$arrayResult = mysql_fetch_array($query);
		return $arrayResult;	
	}
	
	public static function numRows($query){
		$numResult = mysql_num_rows($query);
		return $numResult;	
	}
	
	public function getLastValue($tableName="", $pk=""){
		$query = "select max(".$pk.") as ".$pk." from ".$tableName;
		$arrayOutput = Database::fetchArray(Database::query($query));
		return $arrayOutput[$pk];
	}
	
	public static function setLastTable($tableName){
		$this->lastTable = $tableName;
	}
	
	public static function setFieldsTable($tableName){
		$query = "show fields from ".$tableName;
		$result = Database::query($query);
		
		$arrayFields = array();
		$selectField = array("Field","Type");
		while($arrayResult = Database::fetchArray($result)){
			foreach($arrayResult as $key => $value){
				if(in_array($key, $selectField)){
					$arrayFields[$arrayResult["Field"]] = $arrayResult["Type"];
				}
			}
		}
		return $arrayFields;
	}
	
	public static function getInsertData($arrayInsertData = array(), $tableName = "" , $pk =""){
		Database::$lastTable = $tableName;		
	
		$i = 1;
		$countArray = count($arrayInsertData);
		$fieldName = "";
		$values = "";
		$arrayNoSingleQuote = array("int","numeric","float");
		$arrayFields = Database::setFieldsTable($tableName);

		foreach($arrayInsertData as $key => $value){			
				$Fields = explode('(', $arrayFields[$key]);
				if($pk != ""){					
					Database::$lastPK = Database::getLastValue($tableName,$pk)+1;					
					$fieldName .= $pk.",";
					$values .= Database::$lastPK.",";
					if(in_array($Fields[0],$arrayNoSingleQuote)){
						$values .= $value;
					}else{
						$values .= "'".$value."'";
					}
				}else{
					if(in_array($Fields[0],$arrayNoSingleQuote)){
						$values .= $value;
					}else{
						$values .= "'".$value."'";
					}
				}
				
				$fieldName .= $key;			
				if($i != $countArray){
					$fieldName .= ",";
					$values .= ",";
				}
				$i++;		
		}
		
		$query = "insert into ".$tableName."(".$fieldName.") values(".$values.")";
		return $query;
	}

	public static function getUpdateData($arrayUpdateData = array(), $tableName = "", $whereCondition = ""){
		Database::$lastTable = $tableName;		
		$i = 1;
		$countArray = count($arrayUpdateData);
		$fieldName = "";
		$values = "";
		$arrayNoSingleQuote = array("int","numeric","float");
		$arrayFields = Database::setFieldsTable($tableName);	
		foreach($arrayUpdateData as $key => $value) {
				$Fields = explode('(', $arrayFields[$key]);
				if(in_array($Fields[0],$arrayNoSingleQuote)){
					$values .= $key."=".$value;
				}else{
					$values .= $key."='".$value."'";
				}			
						
				if($i != $countArray){
					$values .= ",";
				}
				$i++;
		}
		
		$query = "update ".$tableName." set ".$values;
		if($whereCondition != ""){
			$query .= " where ".$whereCondition;
		}
		
		return $query;
	}
}
?>